using System;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Ventigrate.Shared.ExternalMembership.Internal;

namespace Ventigrate.Shared.ExternalMembership.WebControls
{
    /// <summary>
    /// Code behind for RolesNew.aspx
    /// </summary>
    public class RoleNew : LayoutsPageBase
    {
        protected InputFormTextBox txtRole;
        protected Label lblMessage;

        protected override bool RequireSiteAdministrator
        {
            get { return true; }
        }

        protected override void OnLoad(EventArgs e)
        {
            // init
            this.CheckRights();
        }

        protected void OnSubmit(object sender, EventArgs e)
        {
            // add the role to the membership provider
            if (!Roles.RoleExists(txtRole.Text))
            {
                try
                {
                    Roles.CreateRole(txtRole.Text);
                    // redirect to roles list
                    Response.Redirect("RolesDisp.aspx", false);
                }
                catch (Exception ex)
                {
                    ULS.Log.Error(ex);
                    SPUtility.TransferToErrorPage(ex.Message); 
                }
            }
            else
            {
                lblMessage.Text = "Role Already Exists";

            }
        }
    }
}
